home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / COMMAND.CPP < prev    next >
C/C++ Source or Header  |  1991-04-28  |  6KB  |  206 lines

  1. #include "stdio.h"
  2. #include "iostream.h"
  3. #include "ctype.h"
  4. #include "string.h"
  5. #include "flyaway.h"
  6.  
  7. // This file does all of the input command parsing and determining
  8. //   if there is a single noun and a single verb, or one of each.
  9. //   If there is a valid input sequence, the words are returned to
  10. //   the calling program by the following function.
  11.  
  12.  
  13.  
  14.  
  15.      // This function gets the noun and verb for each action.  Only
  16.      //  valid verbs and nouns can be returned since they are
  17.      //  returned as enumeration types.  Of course some verb-noun
  18.      //  combinations are illegal, such as "read keys", but these
  19.      //  illegal combinations are handled at the using locations.
  20.  
  21. void get_command(word &verb, word &noun)
  22. {
  23. word wd1, wd2;
  24.  
  25.    verb = (enum word)0;
  26.    noun = (enum word)0;
  27.    read_a_line(wd1, wd2);              // Get a line from the player
  28.    if (wd1) {                          // If there is a value for wd1
  29.       if (is_a_verb(wd1)) verb = wd1;  //   it is a verb
  30.       if (is_a_noun(wd1)) noun = wd1;  //   or a noun.
  31.    }
  32.  
  33.    if (wd2) {                          // If there is a value for wd2
  34.       if (is_a_verb(wd2)) { 
  35.          if (verb == 0)
  36.             verb = wd2;                // it is a verb
  37.          else {
  38.         verb = noun = (enum word)0;           // Two verbs, illegal
  39.         printf("Two verbs are illegal, ignored!\n");
  40.      }
  41.       }
  42.       if (is_a_noun(wd2)) {
  43.          if (noun == 0)
  44.             noun = wd2;                //  It is a noun.
  45.          else {
  46.         verb = noun = (enum word)0;
  47.         printf("Two nouns are illegal, ignored!\n");
  48.      }
  49.       }
  50.    }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.      // This function reads words in ASCII form from the monitor
  57.      //  ignoring any words after two have been read.  The words
  58.      //  are checked to see if they are in the dictionary as de-
  59.      //  fined by the enumeration variable named "word".
  60.  
  61. void read_a_line(word &wd1, word &wd2)
  62. {
  63. char string1[25], string2[25], string3[25];
  64. char last_char;
  65.  
  66.    last_char = get_an_ASCII_word(string1);    // Get first word
  67.    if (last_char != '\n') {
  68.       last_char = get_an_ASCII_word(string2); // Get second word
  69.       while (last_char != '\n') {    // Ignore all trailing words
  70.          last_char = get_an_ASCII_word(string3);
  71.       } 
  72.    } else {
  73.       string2[0] = 0;                         // No second word
  74.    }
  75.  
  76.    wd1 = (enum word)find_in_dictionary(string1);
  77.    wd2 = (enum word)find_in_dictionary(string2);
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.      // This function reads a string, after ignoring the leading
  85.      //  blanks.  The string is terminated when any character is
  86.      //  read that is not alphabetic.  All characters are converted
  87.      //  to lower case for internal use to allow typing flexibility.
  88.  
  89. int get_an_ASCII_word(char in_string[])
  90. {
  91. int char_count = 0;
  92. int char_found = FALSE;
  93. char c;
  94.  
  95.    for (int index = 0 ; index < 80 ; index++) {
  96.       c = tolower(getchar());
  97.       if (c == '\n') {                  // End of line found
  98.          in_string[char_count] = 0;
  99.          return c;
  100.       }
  101.       if (isalpha(c) && char_count < 25) {
  102.          in_string[char_count++] = c;
  103.          char_found = TRUE;
  104.       } else {
  105.          if (isspace(c) && !char_found)
  106.             ;                           // Ignore leading blanks
  107.          else {
  108.             in_string[char_count] = 0;  // ASCIIZ terminator
  109.             return c;
  110.          }
  111.       }
  112.    }
  113. }
  114.  
  115.  
  116.  
  117.  
  118.      // This function uses the dictionary pairs to convert the 
  119.      //  ASCII input strings into the internal enumeration values.
  120.      //  This list must be maintained along with the enumerated
  121.      //  type "word".
  122.  
  123. struct dict_pair {
  124.    char dict_string[10];
  125.    word found_word;
  126. };
  127.  
  128. dict_pair pair[] = {"north"    ,north,
  129.                     "n"        ,north,
  130.                     "east"     ,east,
  131.                     "e"        ,east,
  132.                     "south"    ,south,
  133.                     "s"        ,south,
  134.                     "west"     ,west,
  135.                     "w"        ,west,
  136.                     "drop"     ,drop,
  137.                     "get"      ,get,
  138.                     "look"     ,look,
  139.                     "inventory",inventory,
  140.                     "read"     ,read,
  141.                     "buy"      ,buy,
  142.                     "help"     ,help,
  143.                     "quit"     ,quit,
  144.                     "keys"     ,keys,
  145.                     "candy"    ,candy,
  146.                     "ticket"   ,ticket,
  147.                     "money"    ,money,
  148.                     "monitor"  ,monitor,
  149.                     "paper"    ,paper,
  150.             ""         ,(enum word)0 };   // List terminator
  151.  
  152. int find_in_dictionary(char in_string[])
  153. {
  154. int wd;
  155. dict_pair *pointer = &pair[0];
  156.  
  157.    if (in_string[0] == 0) return 0; // No string to look up
  158.    do {
  159.       if (strcmp(in_string, pointer->dict_string) == 0)
  160.          return pointer->found_word;
  161.       pointer = pointer + 1;        // Next word in list
  162.    } while (pointer->found_word);   // End of word list
  163.    printf("I don't know what \"%s\" is.\n",in_string);
  164.    return 0;                        // Word not found in list
  165. }
  166.  
  167.  
  168.  
  169.  
  170.      // Is the input word a verb?   TRUE or FALSE
  171.  
  172. int is_a_verb(word input_word)
  173. {
  174.    return ((input_word >= north) && (input_word <= quit));
  175. }
  176.  
  177.  
  178.  
  179.  
  180.      // Is the input word a noun?   TRUE or FALSE
  181.  
  182. int is_a_noun(word input_word)
  183. {
  184.    return ((input_word >= keys) && (input_word <= paper));
  185. }
  186.  
  187.  
  188.  
  189.  
  190.      // Is the input word a direction?  TRUE or FALSE
  191.  
  192. int is_a_direction(word input_word)
  193. {
  194.    return ((input_word >= north) && (input_word <= west));
  195. }
  196.  
  197.  
  198.  
  199.  
  200.      // Is the input word an operation?  TRUE or FALSE
  201.  
  202. int is_an_operation(word input_word)
  203. {
  204.    return ((input_word >= drop) && (input_word <= quit));
  205. }
  206.